<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="queryParam.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="changeInput" />
        <!-- 树形组件 -->
        <el-tree ref="deptTree" node-key="id" :expand-on-click-node="false" default-expand-all highlight-current :data="departData" :props="defaultProps" @current-change="currentNode" />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog=true">excel导入</el-button>
          <el-button size="mini" @click="exportEmploy">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table
          ref="multipleTableRef"
          :header-cell-style="{fontSize: '14px', backgroundColor: '#F5F6F8',color:'#909399'}"
          style="width: 100%"
          :data="employeeList"
        >
          <el-table-column align="center" type="selection" width="55" />
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username?.charAt(0) }} </span>
            </template>
          </el-table-column>
          <el-table-column prop="username" align="center" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" align="center" label="聘用形式">
            <template v-slot="{row}">
              <span>{{ row.formOfEmployment===1?'正式':row.formOfEmployment===2?'非正式':'无' }} </span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" align="center" label="入职时间" sortable width="100px" />
          <el-table-column label="操作" width="200px">
            <template v-slot="{row}">
              <el-button type="text" @click="$router.push(`/employee/detail?id=${row.id}`)">查看</el-button>
              <el-button type="text" @click="addRole(row.id) ">角色</el-button>
              <el-popconfirm title="确认删除该行数据吗?">
                <template #reference>
                  <el-button type="text" @click="deleteEmployee(row.id)">删除 </el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height:60px" type="flex" justify="end" align="middle">
          <el-pagination
            :page-size="queryParam.pagesize"
            :current-page="queryParam.page"
            layout="total, prev, pager, next"
            :total="total"
            @current-change="changePage"
          />
        </el-row>
      </div>
    </div>
    <!-- //excel导入弹层 -->
    <importExcleDialog :show-excel-dialog.sync="showExcelDialog" @uploadSuccess="getEmployeeList" />
    <!-- 添加角色权限弹层 -->
    <el-dialog :visible.sync="showRoleDialog" title="分配角色">
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in enableRoleList" :key="item.id" :label="item.id">{{ item.name }} </el-checkbox>
      </el-checkbox-group>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="mini" type="primary" @click="submitRole">确定</el-button>
          <el-button size="mini" @click="showRoleDialog=false">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartmentList } from '@/api/department'
import { getEmployeeList, exportEmployee, deleteEmployee, getEnableRoleList, getEmployeeDetail, putEmployeeRole } from '@/api/employee'
import { transListToTreeData } from '@/utils/index'
import FileSaver from 'file-saver'
import importExcleDialog from './components/importExcleDialog.vue'
export default {
  name: 'Employee',
  components: {
    importExcleDialog
  },
  data() {
    return {
      departData: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      queryParam: {
        page: 1,
        pagesize: 10,
        keyword: '',
        departmentId: null
      },
      employeeList: [],
      total: 0,
      showExcelDialog: false,
      showRoleDialog: false,
      enableRoleList: [],
      roleIds: [],
      currentId: null
    }
  },
  created() {
    this.getDepartmentList()
    this.getEmployeeList()
  },
  methods: {
    async getDepartmentList() {
      this.departData = transListToTreeData(await getDepartmentList(), 0)
      this.queryParam.departmentId = this.departData[0].id
      this.$nextTick(() => {
        this.$refs.deptTree.setCurrentKey(this.queryParam.departmentId)
      })
    },
    async getEmployeeList() {
      const { total, rows } = await getEmployeeList(this.queryParam)
      this.employeeList = rows
      this.total = total
    },
    currentNode(node) {
      this.queryParam.departmentId = node.id
      this.queryParam.page = 1
      this.getEmployeeList()
    },
    changePage(newPage) {
      this.queryParam.page = newPage
      this.getEmployeeList()
    },
    changeInput(value) {
      // console.log(value)
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParam.keyword = value
        this.queryParam.page = 1
        this.getEmployeeList()
      }, 300)
    },
    async exportEmploy() {
      FileSaver.saveAs(await exportEmployee(), '员工信息表.xlsx')
    },
    async deleteEmployee(id) {
      await deleteEmployee(id)
      if (this.employeeList.length === 1 && this.queryParam.page > 1) this.queryParam.page--
      this.getEmployeeList()
    },
    async addRole(id) {
      this.enableRoleList = await getEnableRoleList()
      const { roleIds } = await getEmployeeDetail(id)
      this.roleIds = roleIds
      this.currentId = id
      this.showRoleDialog = true
    },
    async submitRole() {
      await putEmployeeRole({
        id: this.currentId,
        permIds: this.$refs.permTree.getCheckedKeys()
      })
      this.$message.success('分配角色成功')
      this.showRoleDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  margin: 10px;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
